package com.emerquim.persistence.dao;

// default package


import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * A data access object (DAO) providing persistence and search support for Caso
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see .Caso
 * @author MyEclipse Persistence Tools
 */

public class CasoDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(CasoDAO.class);
	// property constants
	public static final String CAS_APE1 = "casApe1";
	public static final String CAS_APE2 = "casApe2";
	public static final String CAS_NOM1 = "casNom1";
	public static final String CAS_NOM2 = "casNom2";
	public static final String CAS_CLASE_ANIMAL = "casClaseAnimal";
	public static final String CAS_SEXO = "casSexo";
	public static final String CAS_EMBARAZO = "casEmbarazo";

	public void save(Caso transientInstance) {
		log.debug("saving Caso instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public void update(Caso persistentInstance) {
		log.debug("updating Evento instance");
		try {
			getSession().update(persistentInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}


	public void delete(Caso persistentInstance) {
		log.debug("deleting Caso instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Caso findById(long id) {
		log.debug("getting Caso instance with id: " + id);
		try {
			Caso instance = (Caso) getSession().get("com.emerquim.persistence.dao.Caso", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Caso instance) {
		log.debug("finding Caso instance by example");
		try {
			List results = getSession().createCriteria("Caso").add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Caso instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Caso as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	/**
	 * Metodo para encontrar los casos para un medico en especifico
	 * @param pTipoDoc El documento del medico
	 * @param pMedicos La identificacion del medico
	 * @author Felipe Criales
	 */
	public List findByMedico(PTipoDocumento pTipoDocumento, PMedicos pMedicos) {
		log.debug("finding Caso instance with property Medico");
		try {
			String queryString = "from Caso as model where model.PTipoDocumentoByMedTdoId = ? AND model.PMedicos = ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, pTipoDocumento);
			queryObject.setParameter(1, pMedicos);			
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCasApe1(Object casApe1) {
		return findByProperty(CAS_APE1, casApe1);
	}

	public List findByCasApe2(Object casApe2) {
		return findByProperty(CAS_APE2, casApe2);
	}

	public List findByCasNom1(Object casNom1) {
		return findByProperty(CAS_NOM1, casNom1);
	}

	public List findByCasNom2(Object casNom2) {
		return findByProperty(CAS_NOM2, casNom2);
	}

	public List findByCasClaseAnimal(Object casClaseAnimal) {
		return findByProperty(CAS_CLASE_ANIMAL, casClaseAnimal);
	}

	public List findByCasSexo(Object casSexo) {
		return findByProperty(CAS_SEXO, casSexo);
	}

	public List findByCasEmbarazo(Object casEmbarazo) {
		return findByProperty(CAS_EMBARAZO, casEmbarazo);
	}

	public List findAll() {
		log.debug("finding all Caso instances");
		try {
			String queryString = "from Caso";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Caso merge(Caso detachedInstance) {
		log.debug("merging Caso instance");
		try {
			Caso result = (Caso) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Caso instance) {
		log.debug("attaching dirty Caso instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Caso instance) {
		log.debug("attaching clean Caso instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}